home *** CD-ROM | disk | FTP | other *** search
- Path: news.mainelink.net!usenet
- From: dgarrets@mainelink.com (David Garretson)
- Newsgroups: comp.lang.c++
- Subject: Re: C++ for loop
- Date: Fri, 16 Feb 1996 03:43:11 GMT
- Organization: Internet Maine Inc.
- Message-ID: <4g0ue8$ct0@news.mainelink.net>
- References: <4flhm3$t3a@ulysses.midtown.net>
- NNTP-Posting-Host: bath-19.mainelink.net
- X-Newsreader: Forte Free Agent 1.0.82
-
- samwise@midtown.net (Rick) wrote:
-
- >I'm in my second week in a C++ programming class. I like it, its a
- >lot like Pascal. I want to draw a box, so I converted an old Pascal
- >routine that I wrote. I can't find a good explanation on the for
- >command. Wonder what I am doing wrong. I hate to wait for Monday to
- >ask. This is what I am trying:
-
- >for(I=2;I >= 79;I++)
- > {
- > gotoxy(I,1);
- > printf("-"); // #205 print #205
- > };
- >I want to do like a FOR NEXT in BASIC or For Do in Pascal. I want to
- >count from 2 to 79 and store the incremented value each time in I,
- >using it for my gotoxy() screen position
- >Thanks
-
-
-
- >...Give me back my data ya dirtbag machine!
-
- The best way that I know to do this is with a nested for loop. The
- outer for loop determines the height of the box and the inner for loop
- determines what gets displayed on the screen. The following will
- display a box that is filled in to display a box that is not filled in
- you need to add some if, else if statements:
-
- //box filled with X's
-
- for( int i =0; i <= 10; i++) {
- for( int a=0; a<=10; a++) { //prints ten X's
- printf( "X");
- }
- printf("\n"); //starts a new line
- }
-
- To print a 'empty' box you can try this:
-
- for( int i=0; i<=10; i++) {
- if( i==0 || i==10) { //if first line or last
- for( int a=0; a<=10; a++) {
- printf("X"); // fill entire line with X's
- }
- }else { //if any line other than first line or last
- for( int a=0; a<=10; a++) {
- if( a == 0 || a==10) { // and the first or last position in the
- printf("X"); //line print an X
- }
- }
- }
- printf("\n"); //start a new line
- }
-
- The code you have written above would only print a single line of
- dashes, also the l >= 79 will cause the for loop to exit after one
- >for(I=2;I >= 79;I++)
- iteration because it will evaluate as false as soon as it is
- evaluated.
-
-